
var tabsSwiper = new Swiper('.swiper-container',{
	speed:500,
	calculateHeight:true,
	onSlideChangeStart: function(){
		$(".tabs .active").removeClass('active');
		$(".tabs a").eq(tabsSwiper.activeIndex).addClass('active');
		currentIndex = tabsSwiper.activeIndex;
	}
});

$(".tabs a").on('touchstart mousedown',function(e){
	e.preventDefault();
	$(".tabs .active").removeClass('active');
	$(this).addClass('active');
	tabsSwiper.swipeTo($(this).index());
    currentIndex = $(this).index();
});

$(".tabs a").click(function(e){
	e.preventDefault();
});

var myScroll0, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset, generatedCount = 0;
var myScroll1, pullDownEl1, pullDownOffset1, pullUpEl1, pullUpOffset1;
var myScroll2, pullDownEl2, pullDownOffset2, pullUpEl2, pullUpOffset2;
var myScroll3, pullDownEl3, pullDownOffset3, pullUpEl3, pullUpOffset3;
var myScroll4, pullDownEl4, pullDownOffset4, pullUpEl4, pullUpOffset4;
var myScroll=new Array();
var pullUp=new Array();
var listPage=new Array(1, 1, 1, 1, 1);
var listProcess=new Array(false, false, false, false, false);

/**
 * 下拉刷新 （自定义实现此方法）
 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
 */
function pullDownAction (index) {
	setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
		var el, li, i;
		el = document.getElementById('list');

		for (i=0; i<3; i++) {
			li = document.createElement('li');
			li.innerText = 'Generated row ' + (++generatedCount);
			el.insertBefore(li, el.childNodes[0]);
		}

		myScroll[index].refresh();		//数据加载完成后，调用界面更新方法   Remember to refresh when contents are loaded (ie: on ajax completion)
	}, 1000);
}

/**
 * 滚动翻页 （自定义实现此方法）
 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
 */
function pullUpAction (index) {
    //alert("appCount:"+Math.ceil(appCategoryList[currentIndex].appCount/pageCount)+"; "+appCategoryList[currentIndex].appCount+"; "+listPage[currentIndex]);
    if(Math.ceil(appCategoryList[currentIndex].appCount/pageCount)>listPage[currentIndex]){
   		listPage[currentIndex]++;
   		listProcess[currentIndex] = true;
   		getInstallApps();
        getAppList(currentIndex, -1, listPage[currentIndex]);
        setTimeout("loadrefresh(currentIndex)", 1000);
    }else{
        console.log("currentIndex 3:"+currentIndex+";  "+pullUp[currentIndex].querySelector('.pullUpLabel').innerHTML);//刷新完毕需更改
        pullUp[currentIndex].querySelector('.pullUpLabel').innerHTML = '全部加载完成';
    }
}

function pullUpAction4 (index) {
}

function loadrefresh(currentIndex){
    //alert("setTimeout:"+currentIndex+"; "+listProcess[currentIndex]);
     console.log("currentIndex:"+currentIndex);
    if(listProcess[currentIndex] == false){
        getInfoByType(currentIndex);
        myScroll[currentIndex].refresh();		// 数据加载完成后，调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
    }else{
        setTimeout("loadrefresh(currentIndex)", 1000);
    }
}

/**
 * 初始化iScroll控件
 */
function loaded() {
	pullDownEl = document.getElementById('pullDown');
	pullDownOffset = pullDownEl.offsetHeight;
	pullUpEl = document.getElementById('pullUp');
	pullUpOffset = pullUpEl.offsetHeight;

	myScroll0 = new iScroll('wrapper', {
		scrollbarClass: 'myScrollbar', /* 重要样式 */
		useTransition: false, /* 此属性不知用意，本人从true改为false */
		topOffset: pullDownOffset,
		onRefresh: function () {
			if (pullDownEl.className.match('loading')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
			} else if (pullUpEl.className.match('loading')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
			}
		},
		onScrollMove: function () {

			if (this.y > 30 && !pullDownEl.className.match('flip')) {
				pullDownEl.className = 'flip';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
				this.minScrollY = 0;
				//alert("onScrollMove 0: "+this.y+"; "+pullDownEl.className+"; "+this.maxScrollY+"; "+pullUpEl.className+"; "+pullDownOffset+";"+pullUpOffset);
			} else if (this.y < 30 && pullDownEl.className.match('flip')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
				this.minScrollY = -pullDownOffset;
				//alert("onScrollMove 1: "+this.y+"; "+pullDownEl.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl.className);
			} else if (this.y < (this.maxScrollY - 30) && !pullUpEl.className.match('flip')) {
				//alert("onScrollMove 2: "+this.y+"; "+pullDownEl.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl.className);
				pullUpEl.className = 'flip';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 30) && pullUpEl.className.match('flip')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
				this.maxScrollY = pullUpOffset;
				//alert("onScrollMove 3: "+this.y+"; "+pullDownEl.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl.className);
			}
		},
		onScrollEnd: function () {
			if (pullDownEl.className.match('flip')) {
				pullDownEl.className = 'loading';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullDownAction");
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl.className.match('flip')) {
				pullUpEl.className = 'loading';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullUpAction");
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});

    myScroll[0]=myScroll0;
    pullUp[0]=pullUpEl;
	setTimeout(function () { document.getElementById('wrapper').style.left = '0';}, 800);
}


/**
 * 初始化iScroll控件
 */
function loaded1() {
    console.log("loaded 1");
	pullDownEl1 = document.getElementById('pullDown1');
	pullDownOffset1 = pullDownEl1.offsetHeight;
	pullUpEl1 = document.getElementById('pullUp1');
	pullUpOffset1 = pullUpEl1.offsetHeight;

	myScroll1 = new iScroll('wrapper1', {
		scrollbarClass: 'myScrollbar', /* 重要样式 */
		useTransition: false, /* 此属性不知用意，本人从true改为false */
		topOffset: pullDownOffset1,
		onRefresh: function () {
			if (pullDownEl1.className.match('loading')) {
				pullDownEl1.className = '';
				pullDownEl1.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
			} else if (pullUpEl1.className.match('loading')) {
				pullUpEl1.className = '';
				pullUpEl1.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
			}
		},
		onScrollMove: function () {

			if (this.y > 30 && !pullDownEl1.className.match('flip')) {
				pullDownEl1.className = 'flip';
				pullDownEl1.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
				this.minScrollY = 0;
				//alert("onScrollMove 0: "+this.y+"; "+pullDownEl1.className+"; "+this.maxScrollY+"; "+pullUpEl1.className+"; "+pullDownOffset1+";"+pullUpOffset1);
			} else if (this.y < 30 && pullDownEl1.className.match('flip')) {
				pullDownEl1.className = '';
				pullDownEl1.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
				this.minScrollY = -pullDownOffset1;
				//alert("onScrollMove 1: "+this.y+"; "+pullDownEl1.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl1.className);
			} else if (this.y < (this.maxScrollY - 30) && !pullUpEl1.className.match('flip')) {
				//alert("onScrollMove 2: "+this.y+"; "+pullDownEl1.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl1.className);
				pullUpEl1.className = 'flip';
				pullUpEl1.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 30) && pullUpEl1.className.match('flip')) {
				pullUpEl1.className = '';
				pullUpEl1.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
				this.maxScrollY = pullUpOffset1;
				//alert("onScrollMove 3: "+this.y+"; "+pullDownEl1.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl1.className);
			}
		},
		onScrollEnd: function () {
			if (pullDownEl1.className.match('flip')) {
				pullDownEl1.className = 'loading';
				pullDownEl1.querySelector('.pullDownLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullDownAction");
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl1.className.match('flip')) {
				pullUpEl1.className = 'loading';
				pullUpEl1.querySelector('.pullUpLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullUpAction");
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});

    myScroll[1]=myScroll1;
    pullUp[1]=pullUpEl1;
	setTimeout(function () { document.getElementById('wrapper').style.left = '0';}, 800);
}

function loaded2() {
    console.log("loaded 2");
	pullDownEl2 = document.getElementById('pullDown2');
	pullDownOffset2 = pullDownEl2.offsetHeight;
	pullUpEl2 = document.getElementById('pullUp2');
	pullUpOffset2 = pullUpEl2.offsetHeight;

	myScroll2 = new iScroll('wrapper2', {
		scrollbarClass: 'myScrollbar', /* 重要样式 */
		useTransition: false, /* 此属性不知用意，本人从true改为false */
		topOffset: pullDownOffset2,
		onRefresh: function () {
			if (pullDownEl2.className.match('loading')) {
				pullDownEl2.className = '';
				pullDownEl2.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
			} else if (pullUpEl2.className.match('loading')) {
				pullUpEl2.className = '';
				pullUpEl2.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
			}
		},
		onScrollMove: function () {

			if (this.y > 30 && !pullDownEl2.className.match('flip')) {
				pullDownEl2.className = 'flip';
				pullDownEl2.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
				this.minScrollY = 0;
				//alert("onScrollMove 0: "+this.y+"; "+pullDownEl2.className+"; "+this.maxScrollY+"; "+pullUpEl2.className+"; "+pullDownOffset2+";"+pullUpOffset2);
			} else if (this.y < 30 && pullDownEl2.className.match('flip')) {
				pullDownEl2.className = '';
				pullDownEl2.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
				this.minScrollY = -pullDownOffset2;
				//alert("onScrollMove 2: "+this.y+"; "+pullDownEl2.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl2.className);
			} else if (this.y < (this.maxScrollY - 30) && !pullUpEl2.className.match('flip')) {
				//alert("onScrollMove 2: "+this.y+"; "+pullDownEl2.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl2.className);
				pullUpEl2.className = 'flip';
				pullUpEl2.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 30) && pullUpEl2.className.match('flip')) {
				pullUpEl2.className = '';
				pullUpEl2.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
				this.maxScrollY = pullUpOffset2;
				//alert("onScrollMove 3: "+this.y+"; "+pullDownEl2.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl2.className);
			}
		},
		onScrollEnd: function () {
			if (pullDownEl2.className.match('flip')) {
				pullDownEl2.className = 'loading';
				pullDownEl2.querySelector('.pullDownLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullDownAction");
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl2.className.match('flip')) {
				pullUpEl2.className = 'loading';
				pullUpEl2.querySelector('.pullUpLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullUpAction");
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});

    myScroll[2]=myScroll2;
    pullUp[2]=pullUpEl2;
	setTimeout(function () { document.getElementById('wrapper').style.left = '0';}, 800);
}

function loaded3() {
    console.log("loaded 3");
	pullDownEl3 = document.getElementById('pullDown3');
	pullDownOffset3 = pullDownEl3.offsetHeight;
	pullUpEl3 = document.getElementById('pullUp3');
	pullUpOffset3 = pullUpEl3.offsetHeight;

	myScroll3 = new iScroll('wrapper3', {
		scrollbarClass: 'myScrollbar', /* 重要样式 */
		useTransition: false, /* 此属性不知用意，本人从true改为false */
		topOffset: pullDownOffset3,
		onRefresh: function () {
			if (pullDownEl3.className.match('loading')) {
				pullDownEl3.className = '';
				pullDownEl3.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
			} else if (pullUpEl3.className.match('loading')) {
				pullUpEl3.className = '';
				pullUpEl3.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
			}
		},
		onScrollMove: function () {

			if (this.y > 30 && !pullDownEl3.className.match('flip')) {
				pullDownEl3.className = 'flip';
				pullDownEl3.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
				this.minScrollY = 0;
				//alert("onScrollMove 0: "+this.y+"; "+pullDownEl3.className+"; "+this.maxScrollY+"; "+pullUpEl3.className+"; "+pullDownOffset3+";"+pullUpOffset3);
			} else if (this.y < 30 && pullDownEl3.className.match('flip')) {
				pullDownEl3.className = '';
				pullDownEl3.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
				this.minScrollY = -pullDownOffset3;
				//alert("onScrollMove 3: "+this.y+"; "+pullDownEl3.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl3.className);
			} else if (this.y < (this.maxScrollY - 30) && !pullUpEl3.className.match('flip')) {
				//alert("onScrollMove 2: "+this.y+"; "+pullDownEl3.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl3.className);
				pullUpEl3.className = 'flip';
				pullUpEl3.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 30) && pullUpEl3.className.match('flip')) {
				pullUpEl3.className = '';
				pullUpEl3.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
				this.maxScrollY = pullUpOffset3;
				//alert("onScrollMove 3: "+this.y+"; "+pullDownEl3.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl3.className);
			}
		},
		onScrollEnd: function () {
			if (pullDownEl3.className.match('flip')) {
				pullDownEl3.className = 'loading';
				pullDownEl3.querySelector('.pullDownLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullDownAction");
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl3.className.match('flip')) {
				pullUpEl3.className = 'loading';
				pullUpEl3.querySelector('.pullUpLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullUpAction");
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});

    myScroll[3]=myScroll3;
    pullUp[3]=pullUpEl3;
	setTimeout(function () { document.getElementById('wrapper').style.left = '0';}, 800);
}

function loaded4() {
    console.log("loaded 3");
	pullDownEl4 = document.getElementById('pullDown4');
	pullDownOffset4 = pullDownEl4.offsetHeight;
	pullUpEl4 = document.getElementById('pullUp4');
	pullUpOffset4 = pullUpEl4.offsetHeight;

	myScroll4 = new iScroll('wrapper4', {
		scrollbarClass: 'myScrollbar', /* 重要样式 */
		useTransition: false, /* 此属性不知用意，本人从true改为false */
		topOffset: pullDownOffset4,
		onRefresh: function () {
			if (pullDownEl4.className.match('loading')) {
				pullDownEl4.className = '';
				pullDownEl4.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
			} else if (pullUpEl4.className.match('loading')) {
				pullUpEl4.className = '';
				pullUpEl4.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
			}
		},
		onScrollMove: function () {

			if (this.y > 30 && !pullDownEl4.className.match('flip')) {
				pullDownEl4.className = 'flip';
				pullDownEl4.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
				this.minScrollY = 0;
				//alert("onScrollMove 0: "+this.y+"; "+pullDownEl4.className+"; "+this.maxScrollY+"; "+pullUpEl4.className+"; "+pullDownOffset4+";"+pullUpOffset3);
			} else if (this.y < 30 && pullDownEl4.className.match('flip')) {
				pullDownEl4.className = '';
				pullDownEl4.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
				this.minScrollY = -pullDownOffset4;
				//alert("onScrollMove 4: "+this.y+"; "+pullDownEl4.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl4.className);
			} else if (this.y < (this.maxScrollY - 30) && !pullUpEl4.className.match('flip')) {
				//alert("onScrollMove 2: "+this.y+"; "+pullDownEl4.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl4.className);
				pullUpEl4.className = 'flip';
				pullUpEl4.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 30) && pullUpEl4.className.match('flip')) {
				pullUpEl4.className = '';
				pullUpEl4.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
				this.maxScrollY = pullUpOffset4;
				//alert("onScrollMove 4: "+this.y+"; "+pullDownEl4.className.match('flip')+"; "+this.maxScrollY+"; "+pullUpEl4.className);
			}
		},
		onScrollEnd: function () {
			if (pullDownEl4.className.match('flip')) {
				pullDownEl4.className = 'loading';
				pullDownEl4.querySelector('.pullDownLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullDownAction");
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl4.className.match('flip')) {
				pullUpEl4.className = 'loading';
				pullUpEl4.querySelector('.pullUpLabel').innerHTML = '加载中...';
				//alert("onScrollEnd pullUpAction4");
				pullUpAction4();	// Execute custom function (ajax call?)
			}
		}
	});

    myScroll[4]=myScroll4;
    pullUp[4]=pullUpEl4;
//	setTimeout(function () { document.getElementById('wrapper').style.left = '0';}, 800);
}

function refreshScroll(){
    myScroll0.refresh();
    myScroll1.refresh();
    myScroll2.refresh();
    myScroll3.refresh();
    $("#pullUp").removeAttr("style");
}

function load(){
   loaded();
   loaded1();
   loaded2();
   loaded3();
   loaded4();
}

//初始化绑定iScroll控件
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', load, false);